package com.jsbj.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.page.PageDomain;
import com.jsbj.page.TableSupport;
import com.jsbj.tools.BeanUtils;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysDictData;
import com.jsbj.user.entities.SysDictType;
import com.jsbj.user.mapper.SysDictTypeMapper;
import com.jsbj.user.service.ISysDictDataService;
import com.jsbj.user.service.ISysDictTypeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-07
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Override
    public Integer add(SysDictType sysDictType) throws Exception {
        String userId = RequestHeaderUtils.getUserId(request);
        LambdaQueryWrapper<SysDictType> qw = new LambdaQueryWrapper<>();
        qw.eq(SysDictType::getDictTypeKey, sysDictType.getDictTypeKey());
        qw.eq(SysDictType::getDisabled, 0);
        List<SysDictType> sysDictTypes = baseMapper.selectList(qw);
        if (null == sysDictType.getId()) {
            if (CollectionUtils.isNotEmpty(sysDictTypes)) {
                throw new Exception("该字典类型已存在，请重新添加！");
            }
            sysDictType.setCreaterId(userId);
            sysDictType.setCreateTime(new Date());
            sysDictType.setUpdateId(userId);
            sysDictType.setUpdateTime(new Date());
            baseMapper.insert(sysDictType);
        } else {
            if (CollectionUtils.isNotEmpty(sysDictTypes) && !sysDictTypes.get(0).getId().equals(sysDictType.getId())) {
                throw new Exception("该字典类型已存在，请重新添加！");
            }
            SysDictType sysDictType1 = baseMapper.selectById(sysDictType.getId());
            BeanUtils.copyPropertiesIgnoreNull(sysDictType, sysDictType1);
            sysDictType.setUpdateId(userId);
            sysDictType.setUpdateTime(new Date());
            baseMapper.updateById(sysDictType);
        }
        return 1;
    }

    @Override
    public IPage<SysDictType> getList(String inParam) {
        JSONObject object = JSONObject.parseObject(inParam);
        String dictTypeDesc = object.getString("dictTypeDesc");
        String dictTypeKey = object.getString("dictTypeKey");
        LambdaQueryWrapper<SysDictType> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(dictTypeDesc)) {
            qw.like(SysDictType::getDictTypeDesc, dictTypeDesc);
        }
        if (StringUtils.isNotEmpty(dictTypeKey)) {
            qw.like(SysDictType::getDictTypeKey, dictTypeKey);
        }
        qw.eq(SysDictType::getDisabled, 0);
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPage();
        Integer pageSize = pageDomain.getPageSize();
        Page<SysDictType> page = new Page<>(pageNum, pageSize);
        IPage<SysDictType> iPage = baseMapper.selectPage(page, qw);
        return iPage;
    }

    @Override
    public Integer delete(String id) throws Exception {
        String userId = RequestHeaderUtils.getUserId(request);
        SysDictType sysDictType = baseMapper.selectById(id);
        LambdaQueryWrapper<SysDictData> qw = new LambdaQueryWrapper<>();
        qw.eq(SysDictData::getDictKey, sysDictType.getDictTypeKey());
        qw.eq(SysDictData::getDisabled, 0);
        List<SysDictData> dictDataList = sysDictDataService.list(qw);
        if (CollectionUtils.isNotEmpty(dictDataList)) {
            throw new Exception("当前字典类型下存在数据，删除失败!");
        }
        sysDictType.setDisabled(1);
        sysDictType.setUpdateId(userId);
        sysDictType.setUpdateTime(new Date());
        return baseMapper.updateById(sysDictType);
    }

    @Override
    public List<SysDictType> selectData() {
        LambdaQueryWrapper<SysDictType> qw = new LambdaQueryWrapper<>();
        qw.eq(SysDictType::getDisabled, 0);
        return baseMapper.selectList(qw);
    }

    @Override
    public List<SysDictType> getByDicKeys(String dictKey) {
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SysDictType::getDisabled, 0);
        queryWrapper.eq(SysDictType::getDictTypeKey, dictKey);
        return baseMapper.selectList(queryWrapper);
    }
}
